// pages/personal/myfeedback/myfeedback.js
var utils = require('../../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        valueLength: 0,
        value:"",
        currentPage:1,
        tabArr: {
            curHdIndex: 0,
            curBdIndex: 0
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.feedbackList(that.data.currentPage)
    },
    onShow:function(){
       
    },
    //tab切换
    tab: function(e) {
        //var dataId = e.currentTarget.dataset.id;
        var that=this;
        var dataId = e.currentTarget.id;
        var obj = {};
        obj.curHdIndex = dataId;
        obj.curBdIndex = dataId;
        this.setData({
            tabArr: obj,
            currentPage:1,
            feedback:[]
        })
        that.feedbackList(that.data.currentPage)
    },
    //跳入反馈信息
    onResultinfoTap: function(e) {
        wx.navigateTo({
            url: '../resultinfo/resultinfo?id='+e.currentTarget.dataset.id,
        })
    },
    onTextareaTap: function(e) {
        var value = e.detail.value
        this.setData({
            value: value,
            valueLength: value.length
        })
    },
    //图片上传
    uploadImg: function(e) {
        var that = this;
        utils.showLoading();
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function(result) {
                        utils.hideLoading();
                        var res = result.data;
                        //do something
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(res);
                        var imageAll = [];
                        imageAll = that.data.imageAll || [];
                        imageAll.push(res);
                        that.setData({
                            imageAll: imageAll,
                        });
                        console.log(that.data.imageAll)
                    },
                    fail: function(res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },
    //删除评价图片
    onCloseImgTap: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var imageAll = that.data.imageAll;
        for (var i in imageAll) {
            if (imageAll[i].id == imageAll[index].id) {
                imageAll.splice(i, 1)
            }
        }
        that.setData({
            imageAll: imageAll
        })
    },
    feedbackList: function (currentPage){
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/common/feedback_list", {
            page: currentPage
        }, function (res) {
            utils.hideLoading();
            var allpost = that.data.feedback;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas.feedback_list) {
                allpost.push(res.datas.feedback_list[i]);
            }
            if (res.datas.feedback_list.length < 15) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                feedback: allpost,
                no_read_count: res.datas.no_read_count,
                ready: res.datas.length == 0 ? 1 : 2,
            })
            console.log(that.data.feedback);
        })
    },
    /**
* 页面上拉触底事件的处理函数
*/
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.feedbackList(page)
        }

    },
    formSubmit: function (e) {
        var that=this;
        if (that.data.value != "" && that.data.imageAll!=undefined) {
            var all = []
            for (var i in that.data.imageAll) {
                all.push(that.data.imageAll[i].id);
            }
            console.log(all);
            utils.http("/api/v1/common/add_feedback", {
                content: that.data.value,
                images: all
            }, function (res) {
                if (res.state == 1) {
                    wx.showToast({
                        title: '您已发布成功~',
                    })

                  
                }
            })
        } else {
            if (that.data.imageAll == undefined) {
                wx.showToast({
                    title: '请上传反馈图片',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                })
            }
            if (that.data.value == "") {
                wx.showToast({
                    title: '请填写描述问题',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                })
            }
           
        }
    },
 

})